<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组reduce方法</title>
</head>

<body>
  <script>
    // ============================== 数组reduce方法 ==============================
    // 作用：数组求和

    const arr = [1, 2, 3, 4, 5]


    // 语法： arr.reduce(function(prev, item){}, 初始值)

    // 1. 没有初始值 
    const res = arr.reduce((prev, item) => prev + item)
    console.log(res)   //15

    // 2. 有初始值
    const sum = arr.reduce((prev, item) => prev + item, 10)
    console.log(sum)  // 25

  </script>
</body>

</html>